<!--UserModel.php -->
<!-- 模型层，存在数据库的使用 -->
<?php 
require 'Model.php';
class UserModel extends Model{
		/*private $conn;*/
		//构造函数用于建立连接
		function __Construct(){
			parent::__Construct();
		}
		function getUserList(){
			$sql = 'select * from users';
			$data=$this->conn->query($sql);
			return $data;
		}
		function addUsers($data){

//如果选择了头像，则上传文件
if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}

//否则为空
		$user = array('uid'=>'',
		'uname' => trim($_POST['uname']),  
		'password' => md5($_POST['password']),
		'gender' => $_POST['gender'],
		'birthdate' => $_POST['birthdate'],
		'hobby' => implode($_POST['hobby'], ','),
		'degree' => $_POST['degree'],
		'intro' => $_POST['intro'],
		'pic'=>$pic
		);
			$sql = "insert into users values ('$user[uid]','$user[uname]','$user[password]','$user[gender]','$user[birthdate]','$user[hobby]','$user[degree]','$user[intro]','$user[pic]')";

			$result=$this->conn->query($sql);
			return $result;

		}
			function getUserById($id){
				$sql='select * from users where uid='.$id;
				$data= $this->conn->query($sql);
				return $data;
				var_dump($data);
			}
			function Updateuser($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
			//如果选择了头像，则上传文件，否则头像为空
			$user = array(
				'uid' => $_POST['id'],
				'uname' => trim($_POST['uname']),  
				'password' => md5($_POST['password']),
				'gender' => $_POST['gender'],
				'birthdate' => $_POST['birthdate'],
				'hobby' => implode($_POST['hobby'], ','),
				'degree' => $_POST['degree'],
				'intro' => $_POST['intro'],
				'pic' => $pic
				);
			var_dump($user);
			$sql = $sql="update users set uname='$user[uname]',gender='$user[gender]',birthdate='$user[birthdate]',hobby='$user[hobby]',degree='$user[degree]',intro='$user[intro]',pic='$user[pic]' where uid=".$user['uid'];
			var_dump($sql);

			$result = $this->conn->query($sql);
			return $result;
		}
		function Deluser($id){
			$sql = "Delete from users where uid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}


}
 ?>